/**通用工具类*/
function isNull(str){if(str==null||str==''||typeof str=='undefined'){return true;}return false;};function I(id) {return document.getElementById(id);};function V(id) {return I(id).value;};
/*设置按钮可点击状态*/function setButtonStatus(btn, time){btn.disabled = true;setTimeout(function () {btn.disabled = false;}, time);}

/**
 * 显示加载进度条
 * @param loadText 加载提示文字
 */
function showLoading(loadText) {
    $('body').append('<div class="modal fade" id="loadingModal">' +
        '    <div style="width: 200px;height:20px; z-index: 6000; position: absolute; text-align: center; left: 50%; top: 50%;margin-left:-100px;margin-top:-10px">' +
        '        <div class="progress progress-striped active" style="margin-bottom: 0;">' +
        '            <div class="progress-bar" style="width: 100%;"></div>' +
        '        </div>' +
        '        <h5 style="color: #fff;">' + loadText + '...</h5>' +
        '    </div>' +
        '</div>');
    $("#loadingModal").modal({backdrop: 'staticFile', keyboard: false}).modal('show');
}

/**
 * 隐藏加载进度条
 */
function hideLoading() {
    $("#loadingModal").modal('hide');
}

/**
 * 校验日期格式是否正确，仅校验格式：yyyy-mm-dd
 */
function checkDate(dateStr) {
    let dateReg = new RegExp('^[1-2]\\d{3}-(0?[1-9]||1[0-2])-(0?[1-9]||[1-2][1-9]||3[0-1])$');
    return dateReg.test(dateStr);
}

/**
 * 获取当前日期,格式：yyyy-mm-dd
 */
function getCurrentDate() {
    let nowDate = new Date();
    let nowMonth = nowDate.getMonth() + 1,nowDay = nowDate.getDate();
    if (nowMonth < 10){
        nowMonth = '0' + nowMonth;
    }
    if (nowDay < 10){
        nowDay = '0' + nowDay;
    }
    return nowDate.getFullYear() + '-' + nowMonth + '-' + nowDay;
}

/**
 * 获取Grid应该设置的高度
 */
function getGridHeight() {
    //修改表格相关标签的高度
    let documentH = $(document).height(),titleH = 40,pagerH = 48,headerH = 42;
    return documentH - titleH - pagerH - headerH;
}

/**
 * 从数组中删除元素
 * @param arr 数组
 * @param delVal 要删除的元素
 */
function arrayRemoveItem(arr, delVal) {
    if (arr instanceof Array) {
        let index = arr.indexOf(delVal);
        if (index > -1) {
            arr.splice(index, 1);
        }
    }
}

/**
 * 获取iframe高度
 * @returns {number} iframe高度
 */
function getIframeHeight() {
    let topHeight = 45,tabTitleHeight = 35;
    return $(document).height() - topHeight - tabTitleHeight - 40;
}

/**
 * 初始化年份select标签下拉框值
 * @param mostYear 最多显示几个年份
 */
function initSelectYear(mostYear) {
    let thisYear = new Date().getFullYear(), tls = '.year-chart-sl';
    for (let i = 0; i < mostYear; i ++){
        let y = thisYear - i, ck = i == mostYear ? 'checked' : '';
        $(tls).append('<option value="' + y + '" ' + ck + '>' + y + '</option>');
    }
}

/**
 * 将星期中1-7转换为周一到周日
 * @param dayOfWeek 星期中1-7
 */
function getDayWeek(dayOfWeek) {
    let mysqlWeek = [1,2,3,4,5,6,7], cnWeek = ['周一','周二','周三','周四','周五','周六','周日'];
    for (let g = 0; g < mysqlWeek.length; g ++){
        if (mysqlWeek[g] == dayOfWeek){
            return cnWeek[g];
        }
    }
}

/**
 * 通过订单状态码获取状态名称
 * @param statusCode 订单状态码
 */
function getStatusByCode(statusCode) {
    let statusCodeArr = [0,1,2,3,4,5,6,7],
        statusNameArr = ['未抢','已抢','已完成','发布人删除','待确认','已取消','已过期','抢单人删除'];
    for (let g = 0; g < statusCodeArr.length; g ++){
        if (statusCodeArr[g] == statusCode){
            return statusNameArr[g];
        }
    }
    return '';
}
/**
 * 通过性别编码获取性别名称
 * @param sexCode 性别码
 */
function getSexName(sexCode) {
    if (sexCode == '0'){
        return '男';
    }
    return sexCode == '1' ? '女' : '未知';
}

/**
 * js转码，替换掉字符串中的<>等特殊字符串，防止xss攻击
 * @param str 要转码的字符串
 * @returns {string} 转码后的字符串
 */
function htmlEncodeByRegExp(s){
    if(isNull(s)) {
        return "";
    }
    let expArr = [/&/g,/</g,/>/g,/ /g,/\'/g,/\"/g], rsArr = ["&amp;","&lt;","&gt;","&nbsp;","&#39;","&quot;"];
    for (let i = 0; i < expArr.length; i ++){
        s = s.replace(expArr[i],rsArr[i]);
    }
    return s;
}